package com.divemotor.ventarepuestosinternet.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.divemotor.ventarepuestosinternet.dao.MarcaDAO;
import com.divemotor.ventarepuestosinternet.model.Marca;

@SuppressWarnings("unchecked")
@Repository(value = "marcaDAO")
public class MarcaDAOImpl implements MarcaDAO, Serializable {

	
	private static final long serialVersionUID = -5526049843841129108L;
	
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Marca> listar() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Marca.class)
				.addOrder(Order.desc("representanteMarca"))
				.addOrder(Order.asc("nombre"));
				
		return criteria.list();
	}

	@Override
	public List<Marca> listarRepresentados() {
		Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Marca.class)
				.add(Restrictions.eq("representanteMarca", "S"))
				.addOrder(Order.asc("nombre"));
		return criteria.list();
	}

	@Override
	public Marca obtenerPorCodigo(Marca marca) {
		return (Marca) getSessionFactory().getCurrentSession().createQuery("from Marca m where m.codigoMarca = :codigo")
										  .setParameter("codigo", marca.getCodigoMarca()).uniqueResult();
	}

}
